package com.meitutest.autoscript;

import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;

import com.meitutest.autoscript.utils.LongpressEvent;

import org.junit.Before;
import org.junit.Test;

/**
 * Created by CrystalChen on 2017/12/20.
 */

public class OTA {
    UiDevice uiDevice;

    @Before
    public void before() {
        this.uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        MyWatcher watcher = new MyWatcher(uiDevice);
        uiDevice.registerWatcher("mywatcher", watcher);
    }

    @Test
    public void ota() throws Exception {
        uiDevice.executeShellCommand("am start -n com.mediatek.systemupdate/.MainEntry");
        LongpressEvent.longPressMenu(uiDevice);
        Thread.sleep(1000);
        uiDevice.findObject(By.text("手动选择安装包")).click();
        uiDevice.findObject(By.textContains("ota")).click();
        uiDevice.findObject(By.text("确定")).click();
        uiDevice.findObject(By.text("确定")).click();
    }

    @Test
    public void recovery() throws Exception {
        uiDevice.executeShellCommand("am start -n com.android.settings/.Settings");
        Thread.sleep(1000);
        uiDevice.swipe(550, 1500, 550, 400, 100);
        uiDevice.findObject(By.text("通用")).click();
        Thread.sleep(1000);
        uiDevice.swipe(550, 1500, 550, 400, 100);
        Thread.sleep(1000);
        uiDevice.findObject(By.text("恢复出厂设置")).click();
        Thread.sleep(1000);
        uiDevice.findObject(By.text("格式化手机存储")).click();
        Thread.sleep(1000);
        uiDevice.findObject(By.text("恢复手机出厂设置")).click();
        Thread.sleep(1000);
        uiDevice.findObject(By.text("确定")).click();
    }

    @Test
    public void jumpRebootGuider() throws Exception {
        Thread.sleep(500);
        uiDevice.findObject(By.text("下一步")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("同意")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("同意")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("跳过")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("确定")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("下一步")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("下一步")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("跳过")).click();
        Thread.sleep(500);
        uiDevice.findObject(By.text("开始享受吧")).click();
        Thread.sleep(3000);
        uiDevice.wait(Until.hasObject(By.text("星期")),500L);
        setScreenAlwaysOn(uiDevice);
    }

    public void setScreenAlwaysOn(UiDevice uiDevice) throws Exception {
        uiDevice.executeShellCommand("am start -n com.android.dialer/com.meitu.mobile.contacts.dialer.activities.MeiosDialtactsActivity");
        Thread.sleep(2000);
        uiDevice.findObject(By.text("*")).click();
        uiDevice.findObject(By.text("#")).click();
        uiDevice.findObject(By.text("6")).click();
        uiDevice.findObject(By.text("3")).click();
        uiDevice.findObject(By.text("4")).click();
        uiDevice.findObject(By.text("8")).click();
        uiDevice.findObject(By.text("8")).click();
        uiDevice.findObject(By.text("#")).click();
        Thread.sleep(1000);
        uiDevice.findObject(By.text("开发者选项")).click();
        Thread.sleep(1000);
        uiDevice.findObject(By.text("不锁定屏幕")).click();
        Thread.sleep(1000);
    }

}
